package com.gxc.webcore.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.StringUtil;

@Controller("common/viewController")
public class ViewController {

	/**
	 * 出错页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView error(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = new HashMap();
		String error_info = request.getParameter("error_info");
		model.put("error_info", error_info);
		return new ModelAndView("default/error.vm", model);
	}

	/**
	 * 通用提示页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView info(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = new HashMap();
		String info_title = request.getParameter("info_title");
		String info_content = request.getParameter("info_content");
		model.put("info_title", info_title);
		model.put("info_content", info_content);
		return new ModelAndView("default/info.vm", model);
	}

	/**
	 * 错误页面
	 * 
	 * @param request
	 * @param response
	 * @param error_info
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static ModelAndView error(HttpServletRequest request,
			HttpServletResponse response, String error_info) throws Exception {
		String requestMethod = request.getHeader("X-Requested-With");
		String jsonpCallback = request.getHeader("jsonpCallback");
		if (requestMethod != null && requestMethod.equals("XMLHttpRequest")) {
			// normal ajax request
			Map result = new HashMap();
			result.put("status", "failed");
			result.put("data", error_info);
			String json_result = JsonUtil.object2json(result);
			response.getWriter().write(json_result);
			return null;
		} else if (jsonpCallback != null && !jsonpCallback.isEmpty()) {
			// jsonp request
			Map result = new HashMap();
			result.put("status", "failed");
			result.put("data", error_info);
			String json_result = JsonUtil.object2jsonp(result, jsonpCallback);
			response.getWriter().write(json_result);
			return null;
		} else {
			Map model = new HashMap();
			model.put("error_info", error_info);
			return new ModelAndView("default/error.vm", model);
		}
	}

	/**
	 * 通用提示页面
	 * 
	 * @param request
	 * @param response
	 * @param info_title
	 * @param info_content
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static ModelAndView info(HttpServletRequest request,
			HttpServletResponse response, String info_title, String info_content)
			throws Exception {
		String requestMethod = request.getHeader("X-Requested-With");
		String jsonpCallback = request.getHeader("jsonpCallback");
		if (requestMethod != null && requestMethod.equals("XMLHttpRequest")) {
			// normal ajax request
			Map result = new HashMap();
			result.put("status", "failed");
			if (!StringUtil.isTrimEmpty(info_title)) {
				result.put("data", info_title);
			} else if (!StringUtil.isTrimEmpty(info_content)) {
				result.put("data", info_content);
			}
			String json_result = JsonUtil.object2json(result);
			response.getWriter().write(json_result);
			return null;
		} else if (jsonpCallback != null && !jsonpCallback.isEmpty()) {
			// jsonp request
			Map result = new HashMap();
			result.put("status", "failed");
			if (!StringUtil.isTrimEmpty(info_title)) {
				result.put("data", info_title);
			} else if (!StringUtil.isTrimEmpty(info_content)) {
				result.put("data", info_content);
			}
			String json_result = JsonUtil.object2jsonp(result, jsonpCallback);
			response.getWriter().write(json_result);
			return null;
		} else {
			Map model = new HashMap();
			model.put("info_title", info_title);
			model.put("info_content", info_content);
			return new ModelAndView("default/info.vm", model);
		}
	}

}
